package wnhz.com.business;

import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.os.Message;
import android.support.v4.content.ContextCompat;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import wnhz.com.business.base.MyBaseActivity;
import wnhz.com.business.uitls.StatusBarUtil;
import wnhz.com.business.uitls.citychose.CityCodeChoseActivity;

/**
 * Created by lizhenglong on 2017-10-24 0024.
 * 注册填写电话号码和密码
 */

public class RegisterActivty extends MyBaseActivity {
    @BindView(R.id.v_statusbar)
    View vStatusbar;
    @BindView(R.id.tv_countryname)
    TextView tv_countryname;
    @BindView(R.id.img_register_right1)
    ImageView img_registerRight1;
    @BindView(R.id.tv_countrytel)
    TextView tv_countrytel;
    @BindView(R.id.ed_register_tel)//注册登录手机号码
    EditText ed_registerTel;
    @BindView(R.id.ed_loginpwd_pwd)//注册登录密码
    EditText ed_loginpwdPwd;
    @BindView(R.id.ed_register_invitationcode)//邀请码
    EditText ed_registerInvitationcode;
    @BindView(R.id.btn_register)
    Button btn_register;
    @BindView(R.id.tv_register_pact)
    TextView tv_registerPact;
    @BindView(R.id.img_hide_pwd)
    ImageView img_hide_pwd;

    private String code = "";
    private String countryname = "";
    private int RESULT_GETCITYCODE = 200;
    private boolean pwdflag = true;

    @Override
    protected void handleMsg(Message msg) {

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);
        ButterKnife.bind(this);



        //状态栏设置
        StatusBarUtil.setColor(this, ContextCompat.getColor(this, R.color.transparent), 0);
        StatusBarUtil.setDarkMode(this, true);
        StatusBarUtil.immersive(this, 0);

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
//            //透明状态栏
//            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
////            //透明导航栏
////            activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
////            //改变状态栏颜色
////            getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_FULLSCREEN);
            vStatusbar.setVisibility(View.VISIBLE);
            //获取到状态栏的高度
            int statusHeight = getStatusBarHeight();
            //动态的设置隐藏布局的高度
            LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) vStatusbar.getLayoutParams();
            params.height = statusHeight;
            vStatusbar.setLayoutParams(params);
        }



        //根据输入框内容来设置登录按钮的背景
        ed_loginpwdPwd.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (!TextUtils.isEmpty(ed_registerTel.getText().toString())) {
                    if (count == 0) {
                        btn_register.setBackground(getResources().getDrawable(R.drawable.bg_circle_grayd));
                        btn_register.setEnabled(false);
                        btn_register.setClickable(false);
                    } else {
                        btn_register.setBackground(getResources().getDrawable(R.drawable.bg_circle_reds));
                        btn_register.setEnabled(true);
                        btn_register.setClickable(true);
                    }
                }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
    }

    @OnClick({R.id.rl_register_back, R.id.rl_chose_country, R.id.img_hide_pwd,R.id.btn_register})
    void click(View view) {

        switch (view.getId()) {
            case R.id.rl_register_back://返回
                finish();
                break;
            case R.id.rl_chose_country://选择国家地区和国家电话区号
                Intent intent = new Intent(RegisterActivty.this, CityCodeChoseActivity.class);
                startActivityForResult(intent, RESULT_GETCITYCODE);
                break;
            case R.id.img_hide_pwd://隐藏和显示密码
                if (pwdflag) {
                    pwdflag = false;
                    img_hide_pwd.setImageResource(R.drawable.ic_login_show);//设置显示密码的图片
                    ed_loginpwdPwd.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
                    ed_loginpwdPwd.setSelection(ed_loginpwdPwd.getText().toString().length()); //调整光标的位置到最后
                } else {
                    pwdflag = true;
                    img_hide_pwd.setImageResource(R.drawable.ic_login_notshow);//设置隐藏密码的图片
                    ed_loginpwdPwd.setTransformationMethod(PasswordTransformationMethod.getInstance());
                    ed_loginpwdPwd.setSelection(ed_loginpwdPwd.getText().toString().length());
                }
                break;
            case R.id.btn_register:
                if (TextUtils.isEmpty(ed_registerTel.getText().toString())){
                    MyToast("请输入电话号码");
                    return;
                }
                if (TextUtils.isEmpty(ed_loginpwdPwd.getText().toString())){
                    MyToast("请设置登录密码");
                    return;
                }
                jumptoNext();//跳转到获取验证码界面
                break;
            default:
                break;
        }
    }

    private void jumptoNext() {
        Intent intent = new Intent(RegisterActivty.this,RegisterGetCodeActivtity.class);
        intent.putExtra("phone",ed_registerTel.getText().toString());
        intent.putExtra("pwd",ed_loginpwdPwd.getText().toString());
        intent.putExtra("invitationcode",ed_registerInvitationcode.getText().toString());
        intent.putExtra("ptype",tv_countrytel.getText().toString());
        startActivity(intent);
    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == RESULT_GETCITYCODE && resultCode == 0) {
            if (data != null) {
                /**
                 * 获取国家地区的电话区号和国家名称
                 */
                code = data.getStringExtra("code");
                countryname = data.getStringExtra("area");
                tv_countryname.setText(countryname);
                tv_countrytel.setText(code);
            }
        }
    }
}
